import api from './index'

// 模型提供商列表查询参数
export interface AgentModelProviderPageRequest {
    page: number
    pageSize: number
    sort?: string
    order?: 'asc' | 'desc'
    keyword?: string
}

// 模型提供商信息
export interface AgentModelProviderInfo {
    id: number
    name: string
    baseUrl: string // 改为baseUrl
    authToken: string
    apiVersion?: string // 新增字段
    rateLimit: number
    costPerToken: number
    maxTokens: number
    timeout: number
    temperature: number
    isDefault: boolean
    priority: number
    role: number
    aiModel: string
    logo: string
    description: string
    status: number
    createdAt: string
    updatedAt: string
}

// 创建模型提供商参数（不应包含 id）
export interface AgentModelProviderCreateDTO {
    // 移除 id 字段
    name: string
    baseUrl: string
    authToken: string
    apiVersion?: string
    rateLimit: number
    costPerToken: number
    maxTokens: number
    timeout: number
    temperature: number
    priority: number
    role: number
    aiModel: string
    logo: string
    description: string
    status: number
}

// 更新模型提供商参数
export interface AgentModelProviderUpdateDTO {
    id: number
    name: string
    baseUrl: string
    authToken: string
    apiVersion?: string
    rateLimit: number
    costPerToken: number
    maxTokens: number
    timeout: number
    temperature: number
    priority: number
    role: number
    aiModel: string
    logo: string
    description: string
    status: number
}

// 分页响应
export interface PageResponse<T> {
    items: T[]
    total: number
    page: number
    pageSize: number
    totalPage: number
}

/**
 * 获取模型提供商列表
 */
export const getAgentModelProviderList = async (params: AgentModelProviderPageRequest) => {
    const response = await api.get<any>('/agent/modelProvider/page', {params})
    return response
}

/**
 * 创建模型提供商
 */
export const createAgentModelProvider = (data: AgentModelProviderCreateDTO) => {
    return api.post('/agent/modelProvider', data)
}

/**
 * 更新模型提供商
 */
export const updateAgentModelProvider = (data: AgentModelProviderUpdateDTO) => {
    return api.put('/agent/modelProvider', data)
}

/**
 * 删除模型提供商
 */
export const deleteAgentModelProvider = (id: number) => {
    return api.delete(`/agent/modelProvider/${id}`)
}

/**
 * 获取模型提供商详情
 */
export const getAgentModelProviderById = (id: number) => {
    return api.get(`/agent/modelProvider/${id}`)
}

/**
 * 设置默认模型提供商
 */
export const setDefaultAgentModelProvider = (id: number) => {
    return api.put(`/agent/modelProvider/default/${id}`)
}

/**
 * 获取默认模型提供商
 */
export const getDefaultAgentModelProvider = () => {
    return api.get('/agent/modelProvider/default')
}

// 模型信息接口
export interface ModelInfo {
    name: string
    description?: string
}

/**
 * 获取活跃模型提供商列表
 */
export const getActiveAgentModelProviders = () => {
    return api.get('/agent/modelProvider/active')
}

/**
 * 根据供应商获取模型列表
 */
export const getModelList = (baseUrl: string, authToken: string) => {
    return api.get<string[]>('/agent/modelProvider/modelList', {
        params: { baseUrl, authToken } // 改为baseUrl
    })
}